package com.zb.code.controller;


import com.zb.annotation.SystemLog;
import com.zb.code.service.ConCodePrintTemplateService;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.SelectVo;
import com.zb.result.Result;
import com.zb.service.ZjCodePrintTemplateService;
import com.zb.utils.TokenUtil;
import com.zb.vo.CodeDataPrintSelectVo;
import com.zb.vo.CodePrintTemplateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 编码打印模板表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2023-09-07
 */
@Api(tags = "编码打印模板类接口")
@RestController
@RequestMapping("/code/printTemplate")
public class ZjCodePrintTemplateController {
    @Resource
    private ZjCodePrintTemplateService codePrintTemplateService;
    @Resource
    private ConCodePrintTemplateService conCodePrintTemplateService;

    @SystemLog(moduleType = OperationLogModuleType.CODE_TYPE, type = OperationLogType.INSERT_UPDATE, remark = "编码档案-编码打印 - 保存或更新打印模板")
    @ApiOperation("保存或更新打印模板")
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody @Valid CodePrintTemplateVo templateVo) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        codePrintTemplateService.save(loginInfo, templateVo);
        return Result.success(null == templateVo.getId()? "新增模板成功" : "修改模板成功");
    }

    @ApiOperation("获取打印模板列表")
    @PostMapping("/getList")
    public Result getList(HttpServletRequest request, @RequestBody SelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取打印模板列表").data(codePrintTemplateService.getList(custAccId, selectVo));
    }

    @ApiOperation("根据模板ID获取模板组件详情")
    @GetMapping("/getDetail/{id}")
    public Result getDetail(@PathVariable @ApiParam(value = "模板ID", required = true) Long id) {
        return Result.success("获取模板组件详情成功").data("row", codePrintTemplateService.getDetailById(id));
    }

    @SystemLog(moduleType = OperationLogModuleType.CODE_TYPE, type = OperationLogType.UPDATE, remark = "编码档案-编码打印 - 根据模板ID批量启用或禁用打印模板")
    @ApiOperation("根据模板ID批量启用或禁用打印模板")
    @PutMapping("/getDetail")
    public Result disable(HttpServletRequest request, @RequestParam @ApiParam(value = "打印模板状态：false-启用，true-禁用", required = true, example = "false") boolean status,
                          @RequestBody @ApiParam(value = "模板ID集合", required = true) List<Long> idList) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        codePrintTemplateService.updateDisabledBatchById(loginInfo, idList, status);
        return Result.success(status? "禁用模板成功" : "启用模板成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.CODE_TYPE, type = OperationLogType.DELETE, remark = "编码档案-编码打印 - 根据模板ID批量删除打印模板")
    @ApiOperation("根据模板ID批量删除打印模板")
    @DeleteMapping("/delete")
    public Result delete(@RequestBody @ApiParam(value = "模板ID集合", required = true) List<Long> idList) {
        codePrintTemplateService.deleteBatchById(idList);
        return Result.success("模板删除成功");
    }

    @ApiOperation("获取打印模板数据")
    @PostMapping("/getPrintTemplateData")
    public Result getPrintTemplateData(@RequestBody @Valid CodeDataPrintSelectVo selectVo) {
        return Result.success("获取打印模板数据成功").data("row", conCodePrintTemplateService.getPrintTemplateData(selectVo));
    }

}
